// 格式化播放量
export const numberFormat = (num: number) => {
  if (num >= 100000000) {
    return (num / 100000000).toFixed(1) + '亿'
  } else if (num >= 10000) {
    return (num / 10000).toFixed(1) + '万'
  } else {
    return num.toString()
  }
}

// 格式化封面大小
export const coverFormat = (url: string, width?: number, height?: number) => {
  return url + '?param=' + width + 'y' + height
}

// 格式需要的歌曲格式
export const songFormat = (song: any) => {
  if (!song.songNmae) {
    const currentAudio = {} as any
    currentAudio.id = song.id
    currentAudio.name = song.name
    currentAudio.picUrl = song.picUrl
    currentAudio.songNmae = song.song.artists[0].name
    return currentAudio
  } else {
    return song
  }
}

export const songFormat2 = (song: any) => {
  const currentAudio = {} as any
  currentAudio.id = song.id
  currentAudio.name = song.name
  currentAudio.picUrl = song.al.picUrl
  currentAudio.songNmae = song.ar[0].name
  return currentAudio
}

// 传入一个数字和一个长度不足这个长度就在前方补0
export const padZero = (num: number | string, length: number) => {
  const n = num.toString()
  return n.padStart(length, '0')
}

// 格式化时间 毫秒 =>  05:12
export const forMatTime = (time: number) => {
  if (!time) return '00:00'
  const minutes = Math.floor(time / 60000)
  const seconds = Math.floor((time % 60000) / 1000)
  return `${minutes >= 10 ? minutes : '0' + minutes}:${seconds >= 10 ? seconds : '0' + seconds}`
}

// 格式化歌词数据
export const formatLyric = (lyric: string) => {
  const lyricArr = lyric.split('\n')
  const lyricObj = lyricArr.map((item) => {
    const timeArr = item.match(/\[(\d{2}):(\d{2})\.(\d{2})\]/)
    if (timeArr) {
      const minutes = parseInt(timeArr[1])
      const seconds = parseInt(timeArr[2])
      const milliseconds = parseInt(timeArr[3])
      const time = minutes * 60 + seconds + milliseconds / 1000
      const text = item.replace(/\[(\d{2}):(\d{2})\.(\d{2})\]/, '').trim()
      return { time, text, active: false }
    }
    return { time: 0, text: '', active: false }
  })
  return lyricObj
}
